<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\House;
use App\Models\Building;
use App\Services\MeterService;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Widgets\Alert;

class HouseController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(
            new House(),
            function (Grid $grid) {
                $grid->id->sortable();
                $grid->status->display(function ($value) {
                    return $value ? '开启' : '关闭';
                });
                $grid->name;
                $grid->address->display(function ($value) {
                    return $value['province'] . $value['city'] . $value['area'] . $value['address'];
                });
                $grid->buildings->display('查看楼栋信息')->expand(\App\Admin\Expand\Building::make(['building_id' => $grid->id]));
                $grid->image->image('', 100);
                $grid->created_at;
                $grid->updated_at->sortable();
                $grid->disableDeleteButton();

                $grid->filter(
                    function (Grid\Filter $filter) {
                        $filter->equal('id');
                    }
                );
                $grid->disableViewButton();
            }
        );
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(
            new House('buildings'),
            function (Form $form) {
                $form->text('name', '房产名称')->required();
                $form->embeds('address', function ($form) {
                    $form->text('province', '省');
                    $form->text('city', '市');
                    $form->text('area', '区');
                    $form->text('address', '详细地址');
                })->required();
                $form->textarea('description', '楼栋描述')->rules('nullable');
                if ($form->isEditing()) {
                    $form->image('image')->rules('nullable');
                }
                $form->hasMany(
                    'buildings',
                    function (Form\NestedForm $nestedForm) use ($form) {
                        $nestedForm->text('name', '楼栋名称')->required();
                        $nestedForm->textarea('description', '楼栋备注');

                        if ($form->isEditing()) {
                            $nestedForm->image('image');
                        }
                        $nestedForm->number('floor')->default(1);
                        $nestedForm->multipleSelect('public_utilities')->options(Building::$publicUtilitiesMap);
                    }
                );
                $info = '<i class="fa fa-exclamation-circle"></i> 请添加楼栋';
                $form->html(Alert::make($info)->info());
                $form->disableViewButton();
                $form->disableDeleteButton();

                $form->saved(function (Form $form, $res) {
                    if ($form->isCreating()) {
                        $HouseModel = \App\Models\House::query()->with('buildings')->find($res);
                        $meterService = new MeterService();
                        // 插入基础电，水表数据
                        $meterService->initMeter($HouseModel, 0, 0);

                        foreach ($HouseModel->buildings as $buildingModel) {
                            $meterService->initMeter($buildingModel, 0, 0);
                        }
                    }
                    return $form->redirect(route('admin.house.index'));
                });
            }
        );
    }
}
